<?php

namespace App\Http\Controllers\Files;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Storage;

class ImgController extends Controller
{
    public function index(Request $request) {

        return view("files/img");
    }

    public function upload(Request $request) {
        if($file = $request->file('upload')) {
            if($file->isValid()){
                $filename = $file->getClientOriginalName();
                $realPath = $file->getRealPath();
                try{
                    $size = getimagesize($realPath);
                    $icode = exif_imagetype($realPath);
                    if(!$icode) {
                        return response()->json(["res"=>"error","msg" => '非法图片']);
                    }
                }catch(Extension $e) {
                    return response()->json(["res"=>"error","msg" => '上传失败!']);
                }
                if(!in_array($file->getClientOriginalExtension(), array('jpg','png','jpeg','gif'))){
                    return response()->json(["res" => 'error',"msg"=>'图片格式错误']);
                }
                if($res = Storage::disk('public')->put('img/qgimg/qrcode_bg.jpg',file_get_contents($realPath))){

                    return response()->json(["msg" => "<div class='dz-size'>上传成功</div>","res"=>'success']);
                }
            } else {
                return response()->json(["msg" => "<div class='dz-size'>上传失败</div>",'res'=>'error']);
            }
        }
    }
}
